﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work02_P1.Mapper;
using Work02_P1.Pojo;
using System.Data;


namespace Work02_P1
{
    public partial class Main : Form
    {
        
        public Main()
        {
            InitializeComponent();
        }

        Form1 login;
        String userId;

        public Main(Form1 form1,String userId)
        {
            InitializeComponent();
            this.login = form1;
            this.ControlBox = false;
            this.userId = userId;
            Loader();
            
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void backToLogin()
        {
            
            login.textBox2.Text = "";
            this.Close();
            MessageBox.Show("退出成功！");
            login.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backToLogin();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Loader();

        }

        public void Loader()
        {
            DataSet dataSet = UserMapper.queryAllContact(userId);
            dataGridView1.DataSource = dataSet.Tables[0];
            dataGridView1.Columns[0].HeaderText = "联系人姓名";
            dataGridView1.Columns[1].HeaderText = "电话";
            //设置数据表格为只读
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String tar = textBox1.Text;
            DataSet dataSet = UserMapper.querySomeContact(userId,tar);
            dataGridView1.DataSource = dataSet.Tables[0];
            dataGridView1.Columns[0].HeaderText = "联系人姓名";
            dataGridView1.Columns[1].HeaderText = "电话";
            //设置数据表格为只读
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Hide();
            new AddContact(this,userId).ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            String name= dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            String phone= dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            if (UserMapper.deleteContact(userId, name, phone))
            {
                MessageBox.Show("删除成功！");
                Loader();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Hide();
            new AddContact(this,userId, dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), 
                dataGridView1.SelectedRows[0].Cells[1].Value.ToString()).ShowDialog();
            
        }
    }
}
